package com.prothink.threadproject.util;

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class FileUploadUtil {

    private static final String UPLOAD_DIR = "D:/download/";
//    private static final long MAX_FILE_SIZE = 500 * 1024; // 500KB
//    private static final String ALLOWED_FILE_TYPE = "image/png";

    public static ResponseEntity<String> uploadFile(MultipartFile file) {
//         检查文件是否为空
//        if (file.isEmpty()) {
//            return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("文件为空，请选择文件上传！");
//        }
//
//        // 验证文件类型
//        if (!ALLOWED_FILE_TYPE.equals(file.getContentType())) {
//            return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("仅支持上传PNG格式文件！");
//        }
//
//        // 验证文件大小
//        if (file.getSize() > MAX_FILE_SIZE) {
//            return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("文件大小不能超过500KB！");
//        }

        // 保存文件
        try {
            String fileName = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date()) + ".png";
            File destinationFile = new File(UPLOAD_DIR + fileName);
            file.transferTo(destinationFile);
            return ResponseEntity.ok("文件上传成功，路径为：" + destinationFile.getAbsolutePath());
        } catch (IOException e) {
            e.printStackTrace();
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("文件上传失败！");
        }
    }
}
